package root.service;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.IAtom;
import com.jfinal.plugin.activerecord.Record;
import root.utils.StringHelper;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class JdbcTemplate {
    private static String dbname = "web";

    public static String s_time = "00:00:00";
    public static String e_time = "23:59:59";

    public static <T> T queryMap(String sql) {
        return Db.use(dbname).queryFirst(sql);
    }

    public static String queryString(String sql) {
        return Db.use(dbname).queryStr(sql);
    }

    public static int queryInt(String sql) {
        return Db.use(dbname).queryInt(sql);
    }

    public static int queryInt(String sql, Object object[]) {
        return Db.use(dbname).queryInt(sql, object);
    }

    public static void insert(String tableName, Record record) {
        Db.use(dbname).save(tableName, record);
    }

    public static void update(String tableName, Record record, String primaryKey, String value) {
        Db.update(tableName, primaryKey, record);
    }

    public static int delete(String tableName, String primaryKey, int userId) {
        return Db.use(dbname).update("update " + tableName + " set del_flag = 1, del_by = " + userId + " where id = " + primaryKey);
    }

    /**
     * 新增或更新(id字段为空新增，否则更新)
     */
    public boolean saveOrUpdate(String tableName, Record record) {
        if (StringHelper.isEmpty(record.getStr("id"))) {
            record.remove("id");
            return Db.save(tableName, record);
        } else {
            return Db.update(tableName, "id", record);
        }
    }

    /**
     * 查询系统参数
     */
    public Record getSystemConfig() {
        return Db.findFirst("select * from t_system_config where status = 0 limit 1");
    }

    /**
     * 事务批量提交
     */
    public boolean batchCommit(final List<String> updateSqls, final Map<String, Record> insertRecord) {
        boolean tx = Db.tx(new IAtom() {
            @Override
            public boolean run() throws SQLException {
                if(updateSqls != null){
                    for (String updateSql : updateSqls) {
                        try {
                            int update = Db.update(updateSql);
                            if (update == 0) {
                                System.out.println("错误sql：" + updateSql);
                                return false;
                            }
                        } catch (Exception e){
                            System.out.println("错误sql：" + updateSql);
                            e.printStackTrace();
                        }
                    }
                }
                if (insertRecord != null) {
                    for (Map.Entry<String, Record> entry : insertRecord.entrySet()) {
                        String key = entry.getKey();
                        String tableName = key.split(":")[0];
                        boolean save = true;
                        try {
                            save = Db.save(tableName, entry.getValue());
                        } catch (Exception e){
                            e.printStackTrace();
                            System.out.println("保存错误表名：" + key + ",错误数据：" + entry.getValue());
                        }
                        if (!save) {
                            System.out.println("保存错误表名：" + key + ",错误数据：" + entry.getValue());
                            return false;
                        }
                    }
                }
                return true;
            }
        });
        return tx;
    }
}
